package org.dromara.business.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.ContentCategoryBo;
import org.dromara.business.domain.vo.ContentCategoryVo;
import org.dromara.business.domain.ContentCategory;
import org.dromara.business.mapper.ContentCategoryMapper;
import org.dromara.business.service.IContentCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 内容分区Service业务层处理
 *
 * @author Hao Tong Xue
 * @date 2025-04-07
 */
@RequiredArgsConstructor
@Service
public class ContentCategoryServiceImpl implements IContentCategoryService {

    private final ContentCategoryMapper baseMapper;

    /**
     * 查询内容分区
     *
     * @param id 主键
     * @return 内容分区
     */
    @Override
    public ContentCategoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询内容分区列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 内容分区分页列表
     */
    @Override
    public TableDataInfo<ContentCategoryVo> queryPageList(ContentCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ContentCategory> lqw = buildQueryWrapper(bo);
        Page<ContentCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的内容分区列表
     *
     * @param bo 查询条件
     * @return 内容分区列表
     */
    @Override
    public List<ContentCategoryVo> queryList(ContentCategoryBo bo) {
        LambdaQueryWrapper<ContentCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ContentCategory> buildQueryWrapper(ContentCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ContentCategory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ContentCategory::getId);
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), ContentCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getCategoryType() != null, ContentCategory::getCategoryType, bo.getCategoryType());
        lqw.eq(bo.getOrderBy() != null, ContentCategory::getOrderBy, bo.getOrderBy());
        lqw.eq(bo.getLevel() != null, ContentCategory::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getHotStatus()), ContentCategory::getHotStatus, bo.getHotStatus());
        lqw.ge(bo.getContentCount() != null, ContentCategory::getContentCount, bo.getContentCount());

        if (bo.getParentId() == null) {
            lqw.isNull(ContentCategory::getParentId);
        }else {
            lqw.eq(ContentCategory::getParentId, bo.getParentId());
        }

        lqw.orderByAsc(ContentCategory::getOrderBy);

        return lqw;
    }

    /**
     * 新增内容分区
     *
     * @param bo 内容分区
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ContentCategoryBo bo) {
        ContentCategory add = MapstructUtils.convert(bo, ContentCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改内容分区
     *
     * @param bo 内容分区
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ContentCategoryBo bo) {
        ContentCategory update = MapstructUtils.convert(bo, ContentCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ContentCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除内容分区信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
